package com.webratio.units.custom.publishnewsunit;
import java.util.Collections;
import java.util.Map;
import org.dom4j.Element;
import com.webratio.rtx.RTXContentUnitService;
import com.webratio.rtx.RTXException;
import com.webratio.rtx.RTXManager;
import com.webratio.rtx.core.AbstractService;
import com.webratio.rtx.core.BeanHelper;
import com.webratio.units.custom.googletranslateunit.GoogleTranslateUnitBean;


public class PublishNewsUnitService extends AbstractService implements RTXContentUnitService{

	private int width;
	private int height;
	private String APIKey;
	
    public PublishNewsUnitService(String id, RTXManager mgr, Element descr) {
        super(id, mgr, descr);
        // TODO Auto-generated constructor stub
        
        Element currentUnit = descr.element("PublishNewsUnit");

		// valores predefinidos
		width = 160;
		height = 60;
		APIKey= "";
        
    }

    public Object computeParameterValue(String outputParamName, Map pageContext, Map sessionContext) throws RTXException {
        // TODO Auto-generated method stub
        return null;
    }

    public Object execute(Map pageContext, Map sessionContext) throws RTXException {
        
    	// bean inizialization
    	PublishNewsUnitBean unitBean = getUnitBean(pageContext, sessionContext);
		if (unitBean == null) {
			return null;
		}

		return unitBean;
    }

    private PublishNewsUnitBean getUnitBean(Map pageContext, Map sessionContext)
	throws RTXException {

		PublishNewsUnitBean unitBean = (PublishNewsUnitBean) pageContext
		.get('_' + getId());
		if (unitBean == null || unitBean.getDataSize() == 0) {
			unitBean = createUnitBean(pageContext, sessionContext);
		}

		/*Estas sgtes lineas son las encargadas de sacar los valores de la URL :P*/
		int widthTmp = Integer.parseInt(BeanHelper.asString(pageContext
				.get(getId() + ".width")));
		int heightTmp = Integer.parseInt(BeanHelper.asString(pageContext
				.get(getId() + ".height")));
		String mashUpCodeTmp = BeanHelper.asString(pageContext
				.get(getId() + ".APIKey"));
		
		
		// Insert of the values
		unitBean.setWidth(widthTmp);
		unitBean.setHeight(heightTmp);
		unitBean.setAPIKey(mashUpCodeTmp);


		pageContext.put('_' + getId(), unitBean);

		return unitBean;
	}
    
    private PublishNewsUnitBean createUnitBean(Map pageContext,
			Map sessionContext) throws RTXException {
    	PublishNewsUnitBean bean = new PublishNewsUnitBean();

		bean.setDataSize(0);
		bean.setData(Collections.EMPTY_LIST);

		return bean;
	}
    
    public void dispose() {
        // TODO Auto-generated method stub
    }

}